using Game.Core;
using Game.Main.Model;
using Game.Manager;
using SimpleJSON;
using UnityEngine;

namespace Game.Component
{
	public class FanItem : BaseItem
	{
		public enum State
		{
			Static = 0,
			Rotate = 1
		}

		private float MAX_SPEED = 360f;

		private float ACCELERATION = 180f;

		private State state;

		private float angle;

		private float speed;

		private Transform fan;

		public override void Init(ItemArchiveData data)
		{
			base.Init(data);
			fan = model.Find("Fan");
			SetState((State)data.state);
			angle = data.intParam1;
			if (angle != 0f)
			{
				fan.eulerAngles = new Vector3(0f, 0f, angle);
			}
		}

		protected override void LateUpdate()
		{
			base.LateUpdate();
			if (state == State.Static)
			{
				if (speed > 0f)
				{
					speed -= Time.deltaTime * ACCELERATION;
					if (speed <= 0f)
					{
						speed = 0f;
					}
					angle -= speed * Time.deltaTime;
					fan.eulerAngles = new Vector3(0f, 0f, angle);
				}
			}
			else
			{
				if (state != State.Rotate)
				{
					return;
				}
				if (speed < MAX_SPEED)
				{
					speed += Time.deltaTime * ACCELERATION;
					if (speed >= MAX_SPEED)
					{
						speed = MAX_SPEED;
					}
				}
				angle -= speed * Time.deltaTime;
				fan.eulerAngles = new Vector3(0f, 0f, angle);
			}
		}

		public override void QuicklyTouch(TouchEventData touchEventData)
		{
			base.QuicklyTouch(touchEventData);
			if (state == State.Static)
			{
				SetState(State.Rotate);
			}
			else if (state == State.Rotate)
			{
				SetState(State.Static);
			}
		}

		private void SetState(State val)
		{
			if (state != val)
			{
				state = val;
				if (state != State.Rotate)
				{
					RemoveAudioPlayer();
				}
				else if (base.itemData.scriptParams.Equals("1"))
				{
					Singleton<AudioManager>.Instance.PlayAudio("sound_fan_1", true, GetAudioPlayer());
				}
			}
		}

		public override void PackData(JSONObject obj)
		{
			base.PackData(obj);
			if (state != 0)
			{
				obj["33"] = (int)state;
			}
			obj["59"] = (int)fan.eulerAngles.z;
		}
	}
}
